package com.leyou.search.listener;

import com.leyou.common.constants.MQConstants;
import com.leyou.search.pojo.Goods;
import com.leyou.search.service.GoodsService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.leyou.common.constants.MQConstants.Exchange.ITEM_EXCHANGE_NAME;
import static com.leyou.common.constants.MQConstants.Queue.SEARCH_ITEM_DOWN;
import static com.leyou.common.constants.MQConstants.Queue.SEARCH_ITEM_UP;

/**
 * @author 黑马程序员
 * @Company http://www.itheima.com
 */
@Component
public class ItemSearchListener {

    @Autowired
    private GoodsService goodsService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = SEARCH_ITEM_UP,durable = "true"),
            exchange = @Exchange(value = ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKey.ITEM_UP_KEY
    ))
    public void addItemtoIndex(Long id) {
        goodsService.addItemToIndex(id);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = SEARCH_ITEM_DOWN,durable = "true"),
            exchange = @Exchange(value = ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKey.ITEM_DOWN_KEY
    ))
    public void deleteItemFromIndex(Long id) {
        goodsService.deleteItemFromIndex(id);
    }
}
